package com.example.helloworld;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class Seekbar extends Activity {

	private View panel;
	private SeekBar seekbar;
	private TextView text;
	private int brightness =	50;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_seekbar);
		
		panel =		findViewById(R.id.panel);
		text =		(TextView)findViewById(R.id.textBrightness);
		seekbar =	(SeekBar)findViewById(R.id.seekBar);
		
		seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
				setBrightness(arg1);
				text.setText("Brightness : " + arg1);
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				hidePanel();
			}
			
		});
	}

	public void openPanel(View view) {
		showPanel();
	}

	private void showPanel() {
		Animation animation =	AnimationUtils.loadAnimation(this, R.anim.translate_left);
		seekbar.setProgress(brightness);
		panel.setVisibility(View.VISIBLE);
		panel.startAnimation(animation);
	}
	
	private void hidePanel() {
		Animation animation =	AnimationUtils.loadAnimation(this, R.anim.translate_right);
		panel.startAnimation(animation);
		panel.setVisibility(View.GONE);
	}
	
	private void setBrightness(int value) {
		if (value < 10) {
			value = 10;
		} else
		if (value > 100) {
			value =	100;
		}
		
		brightness =	value;
		WindowManager.LayoutParams params =	getWindow().getAttributes();
		params.screenBrightness =	(float) value / 100;
		getWindow().setAttributes(params);
	}
}
